package com.hailiao.beidou.message.ui.page.contacts

import androidx.lifecycle.ViewModel
import com.hailiao.beidou.message.data.contact.ContactRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine

/**
 * 联系人
 * @Author d10ng
 * @Date 2024/3/2 15:33
 */
class ContactsScreenViewModel: ViewModel() {
    // 输入搜索内容
    val searchFlow = MutableStateFlow("")
    // 联系人列表
    val dataFlow = ContactRepository.listFlow.combine(searchFlow) { list, search ->
        val s = search.trim()
        if (s.isEmpty()) {
            list
        } else {
            val contain: (String) -> Boolean = { it.contains(s, true) }
            list.filter { contain(it.name) || contain(it.number) || contain(it.pinyin) }
        }
    }

    /**
     * 更新输入内容
     * @param value String
     */
    fun onUpdateSearch(value: String) {
        searchFlow.value = value
    }
}